﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AutoUpdateService.Mgr
{
    using System.Linq;
    using System.Collections;
    using AutoUpdateService.Mgr.Model;
    internal class ClientMgr
    {
        #region Instance

        public static ClientMgr instance { set; get; } = null;

        public static ClientMgr getInstance()
        {
            if (instance == null)
            {
                instance = new ClientMgr();
            }

            return instance;
        }

        #endregion

        private Hashtable clientHashtable { set; get; } = new Hashtable();

        private List<ClientSession> clientList { set; get; } = new List<ClientSession>();

        public void addClient(ClientSession session)
        {
            if (!this.clientHashtable.ContainsKey(session.channelHashCode))
            {
                Console.WriteLine("Client session add");

                this.clientHashtable.Add(session.channelHashCode, session);

                this.clientList.Add(session);
            }
        }

        public void registeClient(int sessionID, string topic)
        {
            if (this.clientHashtable.ContainsKey(sessionID))
            {
                ClientSession session = this.clientHashtable[sessionID] as ClientSession;

                if (session != null)
                {
                    Console.WriteLine("Client session set TOPIC : " + topic);

                    session.Topic = topic;
                }
            }
        }

        public void removeClient(int sessionID)
        {
            if (this.clientHashtable.ContainsKey(sessionID))
            {
                ClientSession session = this.clientHashtable[sessionID] as ClientSession;

                Console.WriteLine("Client session remove");
                this.clientHashtable.Remove(sessionID);

                this.clientList.Remove(session);
            }
        }

        public ClientSession getClient(int sessionID)
        {
            ClientSession result = null;

            if (this.clientHashtable.ContainsKey(sessionID))
            {
                result = this.clientHashtable[sessionID] as ClientSession;
            }

            return result;
        }

        public void transferMessage(AutoUpdate.Model.Packet.Packet packet)
        {
            try
            {
                if (packet.PacketType == AutoUpdate.Model.Packet.PacketType.Request)
                {
                    List<ClientSession> clients = this.clientList.Where(a => a.Topic == packet.Topic).ToList();

                    foreach (var item in clients)
                    {
                        Console.WriteLine("transfer message to topic " + packet.Topic);

                        Server.Server.getInstance().sendMessage(item.channel, PacketMgr.getInstance().getPacketString(packet));
                    }
                }
                if (packet.PacketType == AutoUpdate.Model.Packet.PacketType.Response)
                {
                    ClientSession client = this.clientHashtable[packet.clientSessionID] as ClientSession;

                    Server.Server.getInstance().sendMessage(client.channel, PacketMgr.getInstance().getPacketString(packet));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public List<AutoUpdate.Model.Server.ClientItem> getClientList()
        {
            List<AutoUpdate.Model.Server.ClientItem> result = new List<AutoUpdate.Model.Server.ClientItem>();

            foreach (var item in this.clientList)
            {
                Console.WriteLine(item.channelHashCode);

                AutoUpdate.Model.Server.ClientItem client = new AutoUpdate.Model.Server.ClientItem();

                client.sessionID = item.channelHashCode;

                client.topic = item.Topic;

                client.ipAddress = item.ipAddress;

                result.Add(client);
            }

            return result;
        }

    }
}
